package com.happok.onlineclass.usermanagement.user.controller;

import com.happok.onlineclass.usermanagement.user.dto.UpdateUsersPermissionDTO;
import com.happok.onlineclass.usermanagement.user.dto.UserAuthorityDTO;
import com.happok.onlineclass.usermanagement.user.service.UserPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 用户权限
 * 功能说明
 *
 * @author : chenac
 * @module 在线课堂/用户管理
 * @date : 2021/8/3 15:32
 */
@RestController
@Api(tags = "用户权限")
@RequestMapping(value = "/user-management/management/users/permissions")
public class UserPermissionController {

    @Autowired
    private UserPermissionService userPermissionService;

    /**
     * 更新用户权限
     * 更新用户权限,用户权限不存在则直接插入，用户权限如果存在直接更新或者删除后再更新<br>
     * 20000,更新用户权限成功<br>
     * 40000,参数异常<br>
     * 40402,用户不存在
     *
     * @param updateUsersPermissionDTO
     * @author: chenac
     * @date: 15:50
     * @execption:
     * @return: void
     */
    @PutMapping(value = "/v1")
    @ApiOperation(value = "更新用户权限")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "更新用户权限成功"),
            @ApiResponse(code = 40000, message = "参数异常"),
            @ApiResponse(code = 40402, message = "用户不存在")
    })
    public void updateUserPermission(@RequestBody @Valid UpdateUsersPermissionDTO updateUsersPermissionDTO) {
        userPermissionService.updateUserPermission(updateUsersPermissionDTO);
    }

    /**
     * 查询用户权限
     * 更新用户权限,用户权限不存在则直接插入，用户权限如果存在直接更新或者删除后再更新<br>
     * 20000,更新用户权限成功<br>
     * 40000,参数异常<br>
     * 40402,用户不存在
     *
     * @param user 用户头信息
     * @author: chenac
     * @date: 15:50
     * @execption:
     * @return: void
     */
    @GetMapping(value = "/v1")
    @ApiOperation(value = "查询用户权限")
    @ApiResponses({
            @ApiResponse(code = 20000, message = "查询用户权限成功"),
            @ApiResponse(code = 40000, message = "参数异常"),
            @ApiResponse(code = 40402, message = "用户不存在")
    })
    public UserAuthorityDTO getUserPermission(@RequestHeader String user, HttpServletRequest req) {
        return userPermissionService.getUserPermission(user, req);
    }
}